package com.longtop.perform.zb.team.web;

/**
 * 
 * @author 杨向阳
 * 
 */

import java.util.HashMap;
import java.util.List;

import com.longtop.framework.base.BaseAction;
import com.longtop.framework.common.QueryConditions;
import com.longtop.framework.common.QueryResult;
import com.longtop.framework.util.BeanUtil;
import com.longtop.framework.util.StringUtil;
import com.longtop.framework.util.WebUtil;
import com.longtop.intelliweb.action.IActionContext;
import com.longtop.intelliweb.action.IInitContext;
import com.longtop.intelliweb.view.Dataset;
import com.longtop.perform.zb.index.service.IndexService;
import com.longtop.perform.zb.team.service.TeamService;

public class TeamEditAction extends BaseAction {

	private final static String DATASET_TEAM = "Dataset_Team";
	private final static String DATASET_INDEX = "Dataset_Index";

	/**
	 * 初始化页面
	 * 
	 * @param context
	 */
	public void init(IInitContext context) {
		super.init(context);
	}

	/**
	 * 本页jsp：初始化业务数据(覆写父类方法)
	 */
	public void thisPageInit(IInitContext context) {
		// 初始化数据
		Dataset teamDataset = (Dataset) context.getDataset(DATASET_TEAM);
		Dataset indexDataset = (Dataset) context.getDataset(DATASET_INDEX);

		// 验证数据集对象是否存在
		if (!WebUtil.validateDatasetExist(this, teamDataset, DATASET_TEAM)) {
			return;
		}

		// 页面参数用户Id
		String teamId = context.getRequest().getParameter("teamId");

		// 判断是新增 or 编辑
		if (StringUtil.isEmpty(teamId)) {
		} else {
			QueryConditions queryConditions = WebUtil.createQueryConditions();
			// 调用Spring的业务逻辑类
			TeamService zbTeamService = getBean(TeamService.BEAN_ID);

			IndexService indexService = getBean(IndexService.BEAN_ID);
			queryConditions.setCondition("teamId", QueryConditions.EQUAL, teamId);
			QueryResult queryResult = zbTeamService.loadTeam(queryConditions, 1, 1);
			HashMap hashMap = new HashMap();
			hashMap.put("teamId", teamId);

			List list = indexService.loadIndexNoPage(hashMap);
			try {
				BeanUtil.queryResultToDataset(queryResult, teamDataset, false, true);
				BeanUtil.listToDataset(list, indexDataset, false);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	/**
	 * 保存
	 * 
	 * @param context
	 */
	public void saveTeam(IActionContext context) {

		// 获取页面上的dataset的结构
		Dataset teamIndexDataset = (Dataset) context.getDataset(DATASET_INDEX);

		// 验证数据集是否存在及是否有变化
		if (!WebUtil.validateDatasetChange(this, teamIndexDataset, DATASET_INDEX)) {
			return;
		}

		// 调用Spring的业务逻辑类
		TeamService zbTeamService = getBean(TeamService.BEAN_ID);

		try {
			// 把dataset转成要删除的主键列表、新增bean及更新bean列表
			String teamId = context.getCommandParameter("teamId").getValue();
			List teamIndexList = teamIndexDataset.getRecordList();
			// 调用业务逻辑层的批量更新方法(在一个事务内实现)
			zbTeamService.updateRelation(teamId, teamIndexList);

		} catch (Exception e) {
			// 记入日志
			log.logError(e.getMessage());
			// 在界面显示错误信息
			this.hintErrorMessage(e.getMessage());
		}
	}

}
